package com.shushan.pages;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.net.bean.NetRetBean;
import com.android.net.core.common.RequestType;
import com.android.net.helper.NetHelper;
import com.android.net.listener.common.CallbackCode;
import com.android.net.url.UrlParse;
import com.shushan.base.Constants;
import com.shushan.base.NavigationLoseActivity;
import com.shushan.exam.ExamActivity;
import com.shushan.home.bean.RewardTimeBean;
import com.shushan.home.bean.RewardToyBean;
import com.shushan.network.UrlCommParse;
import com.shushan.network.UrlConst;
import com.shushan.network.listener.NetSingleCommListener;
import com.shushan.pages.bean.TopicBean;
import com.shushan.shushanbao.BabyApplication;
import com.shushan.shushanbao.R;
import com.shushan.util.ImageUtils;
import com.shushan.util.Session;


/**
 * @ClassName: SpecialTopicExerciseActivity
 * @Desciption: //专题练习展示页面
 * @author: zhangshihao
 * @date: 2018-04-14
 */
public class SpecialTopicExerciseActivity extends NavigationLoseActivity implements View.OnClickListener {

    private static final String TAG = SpecialTopicExerciseActivity.class.getSimpleName();

    private ImageView ivToyPic;
    private ImageView ivSpecialTopicImg;

    private TextView tvToyName;
    private TextView tvgetCondition;
    private TextView tvSpecialTopicTitle;
    private TextView tvTeacherName;
    private TextView tvSubjectName;
    private TextView tvTopicType;
    private TextView tvTopicNum;
    private TextView tvSpecialTopicDes;

    private Button btnChangeTeacher;
    private Button btnStartExercise;

    private int awardType;
    private RewardToyBean rewardToyBean;
    private RewardTimeBean rewardTimeBean;
    private BabyApplication shuShanApplication = BabyApplication.getInstance();

    private String change = "0";//1:换一个专题 0首次取到专题
    private int lastSpecialId = 0;//上次取到的专题的专题ID
    private int lastSubjectId = 0;//上次取到的专题的学科ID
    private int changeCount = 0;//点一次重换次数加1首次0

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_special_topic_exercise);

        awardType = getIntent().getIntExtra("awardType",1);
        rewardTimeBean = (RewardTimeBean) Session.getSession().get("rewardTimeBean");
        rewardToyBean = (RewardToyBean) Session.getSession().get("rewardToyBean");

        setTitle("专题练习");

        initView();

        getTopicData();
    }

    private void initView(){
        ivToyPic = findViewById(R.id.iv_toy_pic);
        ivSpecialTopicImg = findViewById(R.id.iv_special_topic_img);

        tvToyName = findViewById(R.id.tv_toy_name);
        tvgetCondition = findViewById(R.id.tv_get_condition);
        tvSpecialTopicTitle = findViewById(R.id.tv_special_topic_name);
        tvTeacherName = findViewById(R.id.tv_teacher_name);
        tvSubjectName = findViewById(R.id.tv_subject_name);
        tvTopicType = findViewById(R.id.tv_topic_type);
        tvTopicNum = findViewById(R.id.tv_topic_num);
        tvSpecialTopicDes = findViewById(R.id.tv_special_topic_desc);

        btnChangeTeacher = findViewById(R.id.btn_change_special_topic);
        btnStartExercise = findViewById(R.id.btn_start_exercise);

        btnChangeTeacher.setOnClickListener(this);
        btnStartExercise.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_change_special_topic:
                getTopicData();
                break;
            case R.id.btn_start_exercise:
                onBackAction();

                Session.getSession().put("rewardTimeBean",rewardTimeBean);
                Session.getSession().put("rewardToyBean",rewardToyBean);

                Intent intent = new Intent(this,ExamActivity.class);
                intent.putExtra("awardType",awardType);
                intent.putExtra("specialId",lastSpecialId);
                startActivityAllow(intent);
                break;
            default:
                break;
        }
    }

    private void getTopicData() {
        int childId = shuShanApplication.getCurrentChildId();

        int practiceType = 1;
        if(rewardTimeBean != null){
            practiceType = rewardTimeBean.practice_type;
        }else if (rewardToyBean != null){
            practiceType = rewardToyBean.practice_type;
        }

        showWaitDialog();

        UrlParse urlParse = new UrlCommParse(this, UrlConst.BASE_URL).appendRegion(UrlConst.GET_ZT_DETAIL);
        UrlParse paramParse = new UrlCommParse(this)
                .putValue("child_id", childId)
                .putValue("practice_type", practiceType)
                .putValue("change", change)
                .putValue("last_special_id", lastSpecialId + "")
                .putValue("last_subject_id", lastSubjectId + "")
                .putValue("change_count", changeCount + "");

        NetHelper.create()
                .url(urlParse.toStringOnlyHeader())
                .param(paramParse.toStringOnlyParam())
                .request(RequestType.REQUEST_TYPE_POST, new NetSingleCommListener<TopicBean>() {
                    @Override
                    protected void onCommon() {
                        dismissWaitDialog();
                    }

                    @Override
                    protected void onSuccess(TopicBean topicBean) {
                        setData(topicBean);
                    }

                    @Override
                    protected void onError(CallbackCode errorCode, NetRetBean netRetBean) {

                        showNetError(netRetBean);
                    }
                });
    }

    private void setData(TopicBean topicBean) {
        if(rewardToyBean == null){
            return;
        }

        ImageUtils.loadWXImageView(this,rewardToyBean.pic,ivToyPic);
        tvToyName.setText(rewardToyBean.name);
        tvgetCondition.setText(rewardToyBean.description);

        if(topicBean == null){
            return;
        }

        change = "1";//1:换一个专题 0首次取到专题
        lastSpecialId = topicBean.id;//上次取到的专题的专题ID
        lastSubjectId = topicBean.subject_id;//上次取到的专题的学科ID
        changeCount++;//点一次重换次数加1首次0

        tvSpecialTopicTitle.setText(topicBean.title);
        tvSpecialTopicDes.setText(topicBean.description);
        tvTeacherName.setText("讲师：" + topicBean.teacher_name);
        if(topicBean.subject_id == Constants.SUBJECT_CHINESE_ID){
            tvSubjectName.setText("科目：语文");
        }else if(topicBean.subject_id == Constants.SUBJECT_MACH_ID){
            tvSubjectName.setText("科目：数学");
        }else if(topicBean.subject_id == Constants.SUBJECT_ENGLISH_ID){
            tvSubjectName.setText("科目：英语");
        }
        tvTopicNum.setText("题数：" + topicBean.exercises_num + "题");

    }
}
